﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using StockCommon.baseclass;
using StockCommon.TaskLog;
using StockImport.http;
using StockCommon.Calendar;

namespace StockImport.dataimport.common
{
    public abstract class DataModuleCommon:DBAccess
    {
        public abstract DateTime getStartDate();
        public abstract DateTime getEndDate();
        public abstract String getTaskName();
        public abstract String getVersion();
        protected abstract ProcessResultEnum UpdateShell(DateTime date, bool force, ref UpdateResult result);
        protected abstract HttpContentFetcher_Daily getFether();
        public abstract bool hasSourceData(DateTime date);
        public abstract Guid getTaskId();
        public virtual bool suitableForDate(DateTime d)
        {
            if ((getStartDate().CompareTo(d) <= 0) &&
                (getEndDate().CompareTo(d) >= 0))
                return true;
            return false;
        }
        protected static String replaceYMD(String s, DateTime date)
        {
            YMD ymd = DateUtils.getYMD(date);
            s = s.Replace("${yyyy}", ymd.yyyy);
            s = s.Replace("${MM}", ymd.MM);
            s = s.Replace("${yy}", ymd.yy);
            s = s.Replace("${dd}", ymd.dd);
            return s;
        }

        public UpdateResult update(DateTime date, bool force = false)
        {
            date = DateUtils.getDateOnly(date);
            UpdateResult result = new UpdateResult();
            result.操作 = getTaskName();
            result.資料日期 = date;


            if (CalenderHelper.isWorkDay(date) == false)
            {
                result.result = true;
                result.Message = "非工作日不需更新";
                return result;
            }

            ProcessResultEnum update = getFether().Update(date, false);
            if ((int)update < 1)
            {
                result.Message = "擷取階段失敗";
                return result;
            }

            if (hasSourceData(date) == false)
            {
                result.Message = "無資料檔案";
                return result;
            }
            StockCommon.ObjectExecLog log = ObjectExecServices.getExecLog(getTaskId(), "imp", date);
            if (force == false)
                if (ObjectExecServices.islogComplete(log))
                {
                    result.result = true;
                    result.Message = "完成且不需更新";
                    return result;
                }
            log.lastExecuteStart = DateTime.Now;


            ProcessResultEnum state = UpdateShell(date, force, ref result);

            log.lastExecuteEnd = DateTime.Now;
            log.status = (int)LogStatus.sUpdated;
            switch (state)
            {
                case ProcessResultEnum.必須重做:
                    log.result = (int)LogResult.rNoData;
                    break;
                case ProcessResultEnum.完成且有資料:
                    log.result = (int)LogResult.rComplete;
                    break;
                case ProcessResultEnum.發生錯誤:
                    log.result = (int)LogResult.rDataError;
                    break;
            }
            log.msg = result.Message;
            ObjectExecServices.updateExecLog(log);
            return result;
        }

        


    }
}
